package com.fll.codeworld.thread;

import lombok.Data;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author fll
 * @description
 * @date 2022/4/27 14:54
 */
@Data
public class Volatile {
    volatile boolean flag = true;
    /**保证原子性**/
    volatile AtomicInteger count  = new AtomicInteger(0);
    /**不保证原子性**/
    //    volatile Integer count  = 0;

    /**
     * 测试volatile的非原子性
     */
    public void m1() {
        for (int i = 0; i < 1000; i++) {
            count.incrementAndGet();
//            count++;
        }
    }

    /**
     * 测试volatile的可见性
     */
    public void m2() {
        System.out.println(Thread.currentThread().getName()+"start");
        while(flag){
            System.out.println(Thread.currentThread().getName()+"running");
        }
        System.out.println(Thread.currentThread().getName()+"stopped");
    }
}
